package cn.dglydrpy.study.mmail.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.dglydrpy.study.mmail.product.entity.AttrEntity;
import cn.dglydrpy.study.mmail.product.service.AttrAttrgroupRelationService;
import cn.dglydrpy.study.mmail.product.service.AttrService;
import cn.dglydrpy.study.mmail.product.service.CategoryService;
import cn.dglydrpy.study.mmail.product.vo.AttrGroupRelationVO;
import cn.dglydrpy.study.mmail.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.dglydrpy.study.mmail.product.entity.AttrGroupEntity;
import cn.dglydrpy.study.mmail.product.service.AttrGroupService;
import cn.dglydrpy.study.mmail.common.utils.PageUtils;
import cn.dglydrpy.study.mmail.common.utils.R;



/**
 * 属性分组
 *
 * @author LY_飘涯
 * @email dglydrpy@qq.com
 * @date 2024-10-12 15:57:11
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    @RequestMapping("/{catelogId}/withattr")
//    @RequiresPermissions("product:attrgroup:info")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> list = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", list);
    }

    /**
     * 列表
     * 
     * 入参：
     *       {
     *           page:1, // 当前页
     *           limit:10, // 每页显示的条数
     *           sidx:"id", // 排序的字段
     *           order:"asc/desc", // 排序的方式
     *           key:"小米" // 查询的关键字
     *       }
     */
    @RequestMapping("/list/{catelogId}")
//    @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params
            ,@PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
//    @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] paths = categoryService.findCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(paths);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }


    /**
     * 关联
     */
    @RequestMapping("/{attrGroupId}/attr/relation")
//    @RequiresPermissions("product:attrgroup:info")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> list = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", list);
    }

    /**
     * 关联删除
     */
    @RequestMapping("/attr/relation/delete")
//    @RequiresPermissions("product:attrgroup:info")
    public R attrRelation(@RequestBody AttrGroupRelationVO[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 未关联属性查询
     */
    @RequestMapping("/{attrGroupId}/noattr/relation")
//    @RequiresPermissions("product:attrgroup:info")
    public R attrNoRelation(@PathVariable("attrGroupId") Long attrGroupId,
                            @RequestParam Map<String, Object> params
        ){
        PageUtils page = attrService.getNoAttrRelation(params,attrGroupId);
        return R.ok().put("page", page);
    }

    @RequestMapping("/attr/relation")
    public R saveBatch(@RequestBody List<AttrGroupRelationVO> vos){
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }
}
